package study.otherpuker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class test {
    List<puke> pukelist=new ArrayList<puke>();//扑克牌列表
    List<player> pList=new ArrayList<player>();//玩家列表
    //创建扑克牌
    public void createpukelist(){
        String[] huastr={"方片","梅花","红桃","黑桃"};
        String[] sustr={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        System.out.println("---------------创建扑克牌-----------------");
        for(int i=0;i<=3;i++){
            for(int j=0;j<13;j++){
                puke puke=new puke(huastr[i]+sustr[j], i, j);
                pukelist.add(puke);
            }
        }
        System.out.println("--------------创建扑克牌成功---------------");
        StringBuilder pkString= new StringBuilder();
        for (puke pk1 : pukelist) {
            pkString.append(pk1.name).append(",");
        }
        System.out.println("扑克牌为："+pkString);
        System.out.println("--------------开始洗牌---------------");
        Collections.shuffle(pukelist);//对List随机排序
        System.out.println("--------------洗牌结束---------------");
    }
    //创建玩家
    public void createplayer(){
        Scanner scanner=new Scanner(System.in);
        int i=0;
        System.out.println("--------------创建玩家---------------");
        while (i<2) {
            System.out.println("请输入玩家ID：");
            if(scanner.hasNextInt()){
                int playerid=scanner.nextInt();
                System.out.println("请输入玩家姓名：");
                String playername=scanner.next();
                player pl=new player(playername, playerid);
                pList.add(pl);
                System.out.println("欢迎玩家:"+pl.name);
                i++;
            }else{
                System.out.println("ID类型错误");
                scanner=new Scanner(System.in);//如果不加该语句Scanner输入的值并不会刷新，所以就会出现无限死循环。
                continue;
            }
        }
    }
    //发牌
    public void putpuke(){
        System.out.println("---------发牌开始-------------");
        int k=0;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                pList.get(j).pukes.add(pukelist.get(k));
                System.out.println("玩家："+pList.get(j).name+"拿牌");
                k++;
            }
        }
        System.out.println(k);
        System.out.println("---------发牌结束-------------");
    }
    public void contrastpuke(){
        List<Integer> maxplids=new ArrayList<Integer>();
        puke p1,p2,maxpuke=new puke();
        p1=contrastPuke(pList.get(0).pukes.get(0),pList.get(0).pukes.get(1));
        p2=contrastPuke(pList.get(1).pukes.get(0),pList.get(1).pukes.get(1));
        System.out.println("玩家"+pList.get(0).name+"最大手牌为："+p1.name);
        System.out.println("玩家"+pList.get(1).name+"最大手牌为："+p2.name);
        maxpuke=contrastPuke(p1,p2);
        if(maxpuke==p1){
            System.out.println("玩家"+pList.get(0).name+"胜利");
        }else{
            System.out.println("玩家"+pList.get(1).name+"胜利");
        }
        for (player player : pList) {
            System.out.println("玩家"+player.name+"的牌为：");
            for(int i=0;i<player.pukes.size();i++){
                System.out.println(player.pukes.get(i).name);
            }
        }
    }
    //比较两个牌的大小，先按照牌的数进行比较，如果数一样，则比较花色大小
    public puke contrastPuke(puke pk1,puke pk2){
        if (pk1.s_id>pk2.s_id) {
            return pk1;
        }else if (pk1.s_id==pk2.s_id) {
            if(pk1.h_id>pk2.h_id){
                return pk1;
            }else{
                return pk2;
            }
        }else {
            return pk2;
        }
    }
    public static void main(String[] args) {
// TODO Auto-generated method stub
        test test=new test();
        test.createpukelist();//创建扑克牌
        test.createplayer();//创建玩家列表
        test.putpuke();//发牌
        test.contrastpuke();//对比
    }
}
